package com.mission.schedule.activity;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.CommonDialog.LocalSynServiceDialog;
import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.WriteAlarmClock;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.service.DownQianDaoService;
import com.mission.schedule.service.UpdataTagService;
import com.mission.schedule.utils.ActivityManager1;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.PhoneFormatCheckUtils;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.Utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by liwei on 2017/8/18.
 */

public class PhoneCodeLogin2Activity extends Activity {

    TextView get_code;
    EditText phone_number;
    Button login_button;
    EditText code;
    TextView login_textview;
    TextView newtitle_tv;
    RelativeLayout top_ll_right;
    LinearLayout top_ll_back;

    boolean isPhoneNumder = false;
    boolean isGetCode = true;
    boolean isCodeNumder = false;
    ActivityManager1 activityManager = null;
    String title = "", isnewyouke = "-1", user;
    int type = 1;
    Context context;
    SharedPrefUtil sharedPrefUtil;
    boolean login = false,welcome = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = this;
        activityManager = ActivityManager1.getInstance();
        activityManager.addActivities(this);

        get_code = (TextView) findViewById(R.id.get_code);
        phone_number = (EditText) findViewById(R.id.phone_number);
        code = (EditText) findViewById(R.id.code);
        login_button = (Button) findViewById(R.id.login_button);
        login_textview = (TextView) findViewById(R.id.login_textview);
        newtitle_tv = (TextView) findViewById(R.id.newtitle_tv);
        top_ll_right = (RelativeLayout) findViewById(R.id.top_ll_right);
        top_ll_back = (LinearLayout) findViewById(R.id.top_ll_back);

        type = getIntent().getIntExtra("type", 1);
        login = getIntent().getBooleanExtra("login", false);
        welcome = getIntent().getBooleanExtra("welcome", false);
        user = getIntent().getStringExtra("user");
        top_ll_back.setVisibility(View.GONE);
//        if (type == 0) {
//            newtitle_tv.setText("手机号注册");
//            login_textview.setText("其他方式注册");
//            top_ll_right.setVisibility(View.VISIBLE);
//        } else {
            newtitle_tv.setText("手机号登录");
            login_textview.setText("其他方式登录");
            top_ll_right.setVisibility(View.GONE);
//        }
        if (welcome){
            top_ll_right.setVisibility(View.VISIBLE);
        } else {
            top_ll_right.setVisibility(View.GONE);
        }
        if (user != null) {
            if (Utils.checkMobilePhone(user)) {
                phone_number.setText(user);
                phone_number.setSelection(user.length());
                get_code.setBackground(getResources().getDrawable(R.drawable.upgrad_button));
                get_code.setTextColor(Color.WHITE);
                get_code.setEnabled(true);
                isPhoneNumder = true;
            }
        }
        progressUtil = new ProgressUtil();
        sharedPrefUtil = new SharedPrefUtil(this, ShareFile.USERFILE);
        String pushAlias = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.PUSH_ALIAS, "0");// 第一次
        if ("0".equals(pushAlias)) {
            // 调用JPush API设置Alias
            String udid = JPushInterface.getRegistrationID(getApplicationContext());
            JPushInterface.setAliasAndTags(getApplicationContext(),udid, null, null);
            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.PUSH_ALIAS, "1");
        }
        isnewyouke = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0");
        initListener();
    }

    CountDownTimer timer = new CountDownTimer(90000, 1000) {
        @Override
        public void onTick(long l) {
            isGetCode = false;
            get_code.setText(String.format(getResources().getString(R.string.get_code), l / 1000));
        }

        @Override
        public void onFinish() {
            isGetCode = true;
            get_code.setText("获取验证码");
            get_code.setBackground(getResources().getDrawable(R.drawable.upgrad_button));
            get_code.setTextColor(Color.WHITE);
            get_code.setEnabled(true);
        }
    };

    @Override
    public void onBackPressed() {
        if (isLogin) {
            type = 0;
            isLogin = false;
            newtitle_tv.setText("手机号登录");
            login_textview.setText("其他方式登录");
            top_ll_right.setVisibility(View.VISIBLE);
        } else {
            finish();
        }
    }

    boolean isLogin = false;

    private void initListener() {
//        top_ll_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                onBackPressed();
//            }
//        });
//        top_ll_right.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (type == 0) {
//                    type = 1;
//                    isLogin = true;
//                    newtitle_tv.setText("手机号注册");
//                    login_textview.setText("其他方式注册");
//                    top_ll_right.setVisibility(View.GONE);
//                }
//            }
//        });
        login_textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (type == 0) {
                    Intent intentlogin = new Intent(context, NewEmailLoginActivity.class);
                    intentlogin.putExtra("login", true);
                    intentlogin.putExtra("login2", login);
                    startActivity(intentlogin);
//                } else {
//                    Intent intentregister = new Intent(context, NewEmailLoginActivity.class);
//                    intentregister.putExtra("login", false);
//                    intentregister.putExtra("login2", login);
//                    startActivity(intentregister);
//                }
            }
        });
        get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone_number.getText().toString().trim().
                        equals(sharedPrefUtil.getString(PhoneCodeLogin2Activity.this, ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
                    normal("该手机号已登录!");
                } else {
                    get_code.setBackground(getResources().getDrawable(R.drawable.get_code_button));
                    get_code.setTextColor(Color.BLACK);
                    get_code.setEnabled(false);
                    code.setFocusable(true);
                    getPhoneCode(phone_number.getText().toString().trim());
                    timer.start();
                }
            }
        });
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCodeNumder && isPhoneNumder) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(code.getWindowToken(), 0);
                    imm.hideSoftInputFromWindow(phone_number.getWindowToken(), 0);
//                    if (type == 0) {
//                        if (phone_number.getText().toString().trim().equals(sharedPrefUtil.getString(PhoneCodeLoginActivity.this, ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
//                            normal("该手机号已登录!");
//                        } else {
//                            loginApp(true);
//                        }
//                    } else {
                        idToPhone();
//                    }
                } else if (!isPhoneNumder) {
                    normal("手机号错误");
                } else if (!isCodeNumder) {
                    normal("验证码错误");
                }
            }
        });
        phone_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                isPhoneNumder = PhoneFormatCheckUtils.isPhoneCode(charSequence.toString());
                if (isPhoneNumder) {
                    if (isGetCode) {
                        get_code.setBackground(getResources().getDrawable(R.drawable.upgrad_button));
                        get_code.setTextColor(Color.WHITE);
                        get_code.setEnabled(true);
                    }
                    if (isCodeNumder) {
                        login_button.setBackground(getResources().getDrawable(R.drawable.upgrad_button));
                        login_button.setEnabled(true);
                    }
                } else {
                    get_code.setBackground(getResources().getDrawable(R.drawable.get_code_button));
                    get_code.setTextColor(getResources().getColor(R.color.gongkai_txt3));
                    get_code.setEnabled(false);
                    login_button.setBackground(getResources().getDrawable(R.drawable.login_button));
                    login_button.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (PhoneFormatCheckUtils.isCode(charSequence.toString()) && isPhoneNumder) {
                    login_button.setBackground(getResources().getDrawable(R.drawable.upgrad_button));
                    login_button.setEnabled(true);
                    isCodeNumder = true;
                    if (!yzm.isEmpty() && code.getText().toString().equals(yzm)) {
                        if (isCodeNumder && isPhoneNumder) {
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(code.getWindowToken(), 0);
                            imm.hideSoftInputFromWindow(phone_number.getWindowToken(), 0);
                            if (type == 0) {
                                if (phone_number.getText().toString().trim().
                                        equals(sharedPrefUtil.getString(PhoneCodeLogin2Activity.this, ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
                                    normal("该手机号已登录!");
                                } else {
//                                    loginApp(true);
                                }
                            } else {
//                                idToPhone();
                            }
                        } else if (!isPhoneNumder) {
                            normal("手机号错误");
                        } else if (!isCodeNumder) {
                            normal("验证码错误");
                        }
                    }
                } else if(PhoneFormatCheckUtils.isCode(charSequence.toString())) {
                    isCodeNumder = true;
                } else {
                    isCodeNumder = false;
                    login_button.setBackground(getResources().getDrawable(R.drawable.login_button));
                    login_button.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!isPhoneNumder) {
                    normal("请输入正确的电话号码");
                }
            }
        });
    }

    private void clearApp() {
        WriteAlarmClock.clearAlarmClock(context);
        if (!"".equals(sharedPrefUtil.getString(context,
                ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
            sharedPrefUtil.putString(context, ShareFile.USERFILE,
                    ShareFile.TELEPHONE, "");
        } else {
            sharedPrefUtil.putString(context, ShareFile.USERFILE,
                    ShareFile.USEREMAIL, "");
        }
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ZDTX, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.BDTX, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISTX, "1");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.QIANDAOSTRING, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISADMIN, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERID, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERBACKGROUNDPATH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERPHOTOPATH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERSTATE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FIRSTLOGIN, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.U_ACC_NO, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.MAXFOCUSID, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.OUTWEEKFAG, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UPDATESETTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UPDATENOTESETTIME, "2017-01-01%2B00:00:00");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNSCHTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNREPTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.RINGSTATE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISYOUKE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNTAGDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.OPENSTYLESTATE, "2");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNSCHTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNRepTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNOldTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PUSH_ALIAS, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.REFRESHFRIEND, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FIRSTDOWNFOCUSSCH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.SHOUCANGDATA, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHARESCHDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHAREREPDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.KuaiJieSouSuo, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.SHAREDATA, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.LOCALPATH, "1");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISSJMOSTIMAGE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE1, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE2, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE3, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE4, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE5, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE6, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE7, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DQRBEFORETIME, "4320");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ZJF, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UNTASKEND, "1");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PERSONREMARK, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PERSONDOWNTIME, "2016-01-01%2B00:00:00");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISNEWYOUKE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.WEEKCOLOR1, "2");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.MONTHCOLOR2, "3");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.YEARCOLOR3, "4");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNTASKTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNRepeatTASKTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.MORNINGTIME, "07:58");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.NIGHTTIME, "20:58");
        ClearMAC();
        // RuntimeCmdManager.clearAppUserData(getPackageName());
        /**
         *
         */
        App.getDBcApplication().douleClear();
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.PERSONDOWNTIME, "2016-01-01");
        Intent intentappwidget = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        Bundle extras = new Bundle();
        intentappwidget.putExtras(extras);
        context.sendBroadcast(intentappwidget);
        activityManager.doAllActivityFinish();
    }

    String yzm = "";

    private void normal(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    private void getPhoneCode(String phonenumer) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("uMobile", phonenumer);
        params.addBodyParameter("type", "6");
        new HttpUtils(5000).send(HttpMethod.POST, URLConstants.短信登录获取验证码2,params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    Gson gson = new Gson();
                    CodeBean bean = gson.fromJson(responseInfo.result, CodeBean.class);
                    if (bean.status == 0) {
                        List<CodeBean.ListBean> list = bean.list;
                        if (list.size() > 0) {
                            yzm = list.get(0).yzm;
                        }
                    }
                    normal("已发送验证码，请注意查看!");
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                normal("网络异常");
            }
        });
    }

    private void loginApp(boolean isLogin) {
        if (isLogin) {
            App.getDBcApplication().douleClear();
            progressUtil.ShowProgress(this, true, true, "正在登录");
        }
        RequestParams params = new RequestParams();
        params.addBodyParameter("uMobile", phone_number.getText().toString().trim());
        params.addBodyParameter("uTocode", "Android");
        params.addBodyParameter("uClintAddr", JPushInterface.getRegistrationID(getApplicationContext()));
        params.addBodyParameter("yzm", code.getText().toString().trim());
        new HttpUtils(5000).send(HttpMethod.POST, URLConstants.手机号快速登录, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                progressUtil.dismiss();
                if (TextUtils.isEmpty(responseInfo.result)) {
                    normal("网络异常");
                } else {
                    Gson gson = new Gson();
                    LoginBean loginBean = gson.fromJson(responseInfo.result, LoginBean.class);
                    if (loginBean.status == 0) {
                        WriteAlarmClock.clearAlarmClock(context);
                        clearApp();
                        List<LoginBean.ListBean> list = loginBean.list;
                        if (list.size() > 0) {
                            sharedPrefUtil.putString(
                                            context,
                                            ShareFile.USERFILE,
                                            ShareFile.NewMyFoundFenXiangFirst,
                                            "1");
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, "1");
                            sharedPrefUtil.putString(
                                            context,
                                            ShareFile.USERFILE,
                                            ShareFile.USEREMAIL,
                                            list.get(0).uEmail);
                            sharedPrefUtil
                                    .putString(
                                            context,
                                            ShareFile.USERFILE,
                                            ShareFile.TELEPHONE,
                                            list.get(0).uMobile);
                            sharedPrefUtil.putString(
                                    context,
                                    ShareFile.USERFILE,
                                    ShareFile.USERID,
                                    list.get(0).uId
                                            + "");
                            sharedPrefUtil.putString(
                                            context,
                                            ShareFile.USERFILE,
                                            ShareFile.USERNAME,
                                            list.get(0).uNickName);
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERSTATE, "1");
                            sharedPrefUtil.putString(
                                            context, ShareFile.USERFILE, ShareFile.USERBACKGROUNDPATH, list.get(0).uBackgroundImage);
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.PERSONREMARK, list.get(0).uPersontag);
                            if (!"".equals(list.get(0).uPortrait)) {
                                String str = list.get(0).uPortrait.toString();
                                str = str.replace("\\", "");
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERPHOTOPATH, str);
                            }
                            sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE, ShareFile.FIRSTOPENAPP, "1");
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.U_ACC_NO, list.get(0).uAccNo);
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, list.get(0).uIsActive + "");
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0");
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "-1");
                            App.getDBcApplication().douleClear();
                            getJson();
                            Intent tagintent = new Intent(context, UpdataTagService.class);
                            tagintent.setAction("UpdataTagService");
                            tagintent.putExtra("down", "downtag");
                            tagintent.setPackage(getPackageName());
                            startService(tagintent);
                            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.FIRSTLOGIN, "0");
                            sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
                            activityManager.doAllActivityFinish();
                            startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                            if (login) {
                                sendBroadcast(new Intent("loginReceiver"));
                            }
                        }
                    } else if (loginBean.status == 1) {
                        normal("没有该用户");
                    } else if (loginBean.status == 6) {
                        normal("验证码超时");
                    } else if (loginBean.status == 7 || loginBean.status == 8) {
                        normal("验证码错误");
                    } else {
                        normal("网络异常");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                progressUtil.dismiss();
                normal("网络异常");
            }
        });
    }

    int heigh = 0;
    ProgressUtil progressUtil;

    private void idToPhone() {
        progressUtil.ShowProgress(this, true, true, "正在登录");
        final RequestParams params = new RequestParams();
        params.addBodyParameter("uMobile", phone_number.getText().toString().trim());
        params.addBodyParameter("uId", sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, ""));
        params.addBodyParameter("uTocode", "Android");
        params.addBodyParameter("pwd", "123456");
        params.addBodyParameter("yzm", code.getText().toString().trim());
        params.addBodyParameter("pushMac", JPushInterface.getRegistrationID(getApplicationContext()));
        params.addBodyParameter("type", "0");
        params.addBodyParameter("uEmail", "");
        new Thread(new Runnable() {
            @Override
            public void run() {
                new HttpUtils(50000).send(HttpMethod.POST, URLConstants.游客注册或登录, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        progressUtil.dismiss();
                        if (TextUtils.isEmpty(responseInfo.result)) {
                            normal("网络异常");
                        } else {
                            Gson gson = new Gson();
                            LoginBean loginBean = gson.fromJson(responseInfo.result, LoginBean.class);
                            if (loginBean.status == 0) {
                                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "").isEmpty()) {
                                    WriteAlarmClock.clearAlarmClock(context);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.NEWYOUKETOUSER, "0");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0");
                                    sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE, ShareFile.FIRSTOPENAPP, "0");
                                    App.getDBcApplication().douleClear();
                                    getJson();
                                } else {
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.NEWYOUKETOUSER, "1");
                                }
                                List<LoginBean.ListBean> list = loginBean.list;
                                if (list.size() > 0) {
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USEREMAIL, list.get(0).uEmail);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TELEPHONE, list.get(0).uMobile);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERID, list.get(0).uId + "");
                                    App.getDBcApplication().updateNoteSate(list.get(0).uId + "");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERNAME, list.get(0).uNickName);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERBACKGROUNDPATH, list.get(0).uBackgroundImage);
                                    if (!"".equals(list.get(0).uPortrait)) {
                                        String str = list.get(0).uPortrait.toString();
                                        str = str.replace("\\", "");
                                        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERPHOTOPATH, str);
                                    }
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERSTATE, "1");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.U_ACC_NO, "12" + list.get(0).uId);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.PERSONREMARK, list.get(0).uPersontag);

                                }
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, "1");
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "-1");
                                Intent tagintent = new Intent(context, UpdataTagService.class);
                                tagintent.setAction("UpdataTagService");
                                tagintent.putExtra("down", "downtag");
                                tagintent.setPackage(getPackageName());
                                startService(tagintent);
                                Intent qdintent = new Intent(context, DownQianDaoService.class);
                                qdintent.putExtra("heigh", heigh);
                                qdintent.putExtra("firstLogin", "0");
                                qdintent.setAction("updateData");
                                qdintent.setPackage(getPackageName());
                                startService(qdintent);

//                                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "").isEmpty()) {
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.FIRSTLOGIN, "0");
                                    sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
                                    sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE, ShareFile.FIRSTOPENAPP, "0");
                                    activityManager.doAllActivityFinish();
                                    startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                                    if (login) {
                                        sendBroadcast(new Intent("loginReceiver"));
                                    }
//                                } else {
//                                    sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE, ShareFile.FIRSTOPENAPP, "1");
//                                    sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
//                                    //不是第一次登陆
//                                    sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.FIRSTLOGIN, "1");
//
//                                    LocalSynServiceDialog localSynServiceDialog = new LocalSynServiceDialog(context, R.style.dialog_translucent, getWindowManager());
//                                    localSynServiceDialog.show();
//
//                                    localSynServiceDialog.setCallBack(new LocalSynServiceDialog.CallBack() {
//                                        @Override
//                                        public void dialogDismiss() {
//                                            startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
//                                            if (login) {
//                                                sendBroadcast(new Intent("loginReceiver"));
//                                            }
//                                            finish();
//                                        }
//                                    });
//                                }
                            } else if (loginBean.status == 1) {
                                normal("没有该手机号");
                            } else if (loginBean.status == 6) {
                                normal("验证码超时");
                            } else if (loginBean.status == 7) {
                                normal("验证码错误");
                            } else if (loginBean.status == 8) {
                                normal("验证码不存在");
                            } else if (loginBean.status == 9) {
                                WriteAlarmClock.clearAlarmClock(context);
                                List<LoginBean.ListBean> list = loginBean.list;
                                if (list.size() > 0) {
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.NewMyFoundFenXiangFirst, "1");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, "1");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USEREMAIL, list.get(0).uEmail);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.PERSONREMARK, list.get(0).uPersontag);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TELEPHONE, list.get(0).uMobile);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERID, list.get(0).uId + "");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERNAME, list.get(0).uNickName);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERSTATE, "1");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERBACKGROUNDPATH, list.get(0).uBackgroundImage);
                                    if (!"".equals(list.get(0).uPortrait)) {
                                        String str = list.get(0).uPortrait.toString();
                                        str = str.replace("\\", "");
                                        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.USERPHOTOPATH, str);
                                    }
                                    sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE, ShareFile.FIRSTOPENAPP, "1");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.U_ACC_NO, list.get(0).uAccNo);
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, list.get(0).uIsActive + "");
                                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.SHUNYANTYPE, "0");
                                    App.getDBcApplication().updateNoteSate(list.get(0).uId + "");
                                }
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISYOUKE, "1");
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "-1");
//                                App.getDBcApplication().douleClear();
                                getJson();
                                Intent tagintent = new Intent(context, UpdataTagService.class);
                                tagintent.setAction("UpdataTagService");
                                tagintent.putExtra("down", "downtag");
                                tagintent.setPackage(getPackageName());
                                startService(tagintent);
                                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.FIRSTLOGIN, "0");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "1");
                                activityManager.doAllActivityFinish();
                                startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                                if (login) {
                                    sendBroadcast(new Intent("loginReceiver"));
                                }
                            } else {
                                normal("网络异常");
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        progressUtil.dismiss();
                        normal("网络异常");
                    }
                });
            }
        }).start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    private void getJson() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder sb = new StringBuilder();
                try {
                    AssetManager assetManager = context.getAssets();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open("cytx.json")));
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        sb.append(line);
                    }
                    sharedPrefUtil.putString(context, ShareFile.USERFILE,
                            ShareFile.CYTXJSON, sb.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void ClearMAC() {
        if (NetUtil.getConnectState(context) != NetUtil.NetWorkState.NONE) {
            if (sharedPrefUtil.getString(context,
                    ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                final String path = URLConstants.修改MAC地址 + sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "") + "&uClintAddr="
                        + "" + "&uTocode=android";
                StringRequest request = new StringRequest(Request.Method.GET, path,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                });
                request.setTag("down");
                request.setRetryPolicy(new DefaultRetryPolicy(20000, 1, 1.0f));
                App.getHttpQueues().add(request);
            }
        } else {
            return;
        }
    }

    private class CodeBean {

        public String downTime;
        public String message;
        public int status;
        public List<ListBean> list;

        public String getDownTime() {
            return downTime;
        }

        public void setDownTime(String downTime) {
            this.downTime = downTime;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public class ListBean {

            public String uname;
            public String yzm;
            public String type;
            public String mobile;

            public String getUname() {
                return uname;
            }

            public void setUname(String uname) {
                this.uname = uname;
            }

            public String getYzm() {
                return yzm;
            }

            public void setYzm(String yzm) {
                this.yzm = yzm;
            }

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }

            public String getMobile() {
                return mobile;
            }

            public void setMobile(String mobile) {
                this.mobile = mobile;
            }
        }
    }

    private class LoginBean {

        /**
         * downTime : 2017-10-25T13:52:27
         * list : [{"oldUid":"0","uAccNo":"121318776","uActivationTime":null,"uBackgroundImage":"20170910/650644.png","uBirthday":"","uClientAddr":"","uCompany":"","uCurPlace":"直辖市-北京","uCurPlacePoint":"101010100","uEmail":"","uFindPwdCode":"","uFindPwdCount":0,"uFindPwdTime":"","uGgender":-1,"uId":1318776,"uIsActive":1,"uLanguageType":"C","uLoginCount":1,"uMobile":"15108409732","uNickName":"Android时间表","uOccupation":0,"uOpAddr":"","uOpAddrBelong":"","uPersontag":"","uPinyin":"A","uPortrait":"20170910/451857.png","uPositions":"","uPwd":"cdf61889b3b01862d4b001a5c30a4d8f","uRegTime":"2017-07-03T12:56:48","uSchedule":"","uSourceType":0,"uSpecialAccount":"","uToCode":"android","uUpdateTime":"2017-10-19T20:19:09","uValiTime":null,"uWeixinOpenid":"ovAvbst_tOeEjkrc8N1AalkJsNFs","uWeixinSign":"bdg","userIdOne":1354122,"userIdTwo":0}]
         * list2 : null
         * message : 成功
         * status : 0
         */

        public String downTime;
        public Object list2;
        public String message;
        public int status;
        public List<ListBean> list;

        public String getDownTime() {
            return downTime;
        }

        public void setDownTime(String downTime) {
            this.downTime = downTime;
        }

        public Object getList2() {
            return list2;
        }

        public void setList2(Object list2) {
            this.list2 = list2;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public class ListBean {
            /**
             * oldUid : 0
             * uAccNo : 121318776
             * uActivationTime : null
             * uBackgroundImage : 20170910/650644.png
             * uBirthday :
             * uClientAddr :
             * uCompany :
             * uCurPlace : 直辖市-北京
             * uCurPlacePoint : 101010100
             * uEmail :
             * uFindPwdCode :
             * uFindPwdCount : 0
             * uFindPwdTime :
             * uGgender : -1
             * uId : 1318776
             * uIsActive : 1
             * uLanguageType : C
             * uLoginCount : 1
             * uMobile : 15108409732
             * uNickName : Android时间表
             * uOccupation : 0
             * uOpAddr :
             * uOpAddrBelong :
             * uPersontag :
             * uPinyin : A
             * uPortrait : 20170910/451857.png
             * uPositions :
             * uPwd : cdf61889b3b01862d4b001a5c30a4d8f
             * uRegTime : 2017-07-03T12:56:48
             * uSchedule :
             * uSourceType : 0
             * uSpecialAccount :
             * uToCode : android
             * uUpdateTime : 2017-10-19T20:19:09
             * uValiTime : null
             * uWeixinOpenid : ovAvbst_tOeEjkrc8N1AalkJsNFs
             * uWeixinSign : bdg
             * userIdOne : 1354122
             * userIdTwo : 0
             */

            public String oldUid;
            public String uAccNo;
            public Object uActivationTime;
            public String uBackgroundImage;
            public String uBirthday;
            public String uClientAddr;
            public String uCompany;
            public String uCurPlace;
            public String uCurPlacePoint;
            public String uEmail;
            public String uFindPwdCode;
            public int uFindPwdCount;
            public String uFindPwdTime;
            public int uGgender;
            public int uId;
            public int uIsActive;
            public String uLanguageType;
            public int uLoginCount;
            public String uMobile;
            public String uNickName;
            public int uOccupation;
            public String uOpAddr;
            public String uOpAddrBelong;
            public String uPersontag;
            public String uPinyin;
            public String uPortrait;
            public String uPositions;
            public String uPwd;
            public String uRegTime;
            public String uSchedule;
            public int uSourceType;
            public String uSpecialAccount;
            public String uToCode;
            public String uUpdateTime;
            public Object uValiTime;
            public String uWeixinOpenid;
            public String uWeixinSign;
            public int userIdOne;
            public int userIdTwo;

            public String getOldUid() {
                return oldUid;
            }

            public void setOldUid(String oldUid) {
                this.oldUid = oldUid;
            }

            public String getUAccNo() {
                return uAccNo;
            }

            public void setUAccNo(String uAccNo) {
                this.uAccNo = uAccNo;
            }

            public Object getUActivationTime() {
                return uActivationTime;
            }

            public void setUActivationTime(Object uActivationTime) {
                this.uActivationTime = uActivationTime;
            }

            public String getUBackgroundImage() {
                return uBackgroundImage;
            }

            public void setUBackgroundImage(String uBackgroundImage) {
                this.uBackgroundImage = uBackgroundImage;
            }

            public String getUBirthday() {
                return uBirthday;
            }

            public void setUBirthday(String uBirthday) {
                this.uBirthday = uBirthday;
            }

            public String getUClientAddr() {
                return uClientAddr;
            }

            public void setUClientAddr(String uClientAddr) {
                this.uClientAddr = uClientAddr;
            }

            public String getUCompany() {
                return uCompany;
            }

            public void setUCompany(String uCompany) {
                this.uCompany = uCompany;
            }

            public String getUCurPlace() {
                return uCurPlace;
            }

            public void setUCurPlace(String uCurPlace) {
                this.uCurPlace = uCurPlace;
            }

            public String getUCurPlacePoint() {
                return uCurPlacePoint;
            }

            public void setUCurPlacePoint(String uCurPlacePoint) {
                this.uCurPlacePoint = uCurPlacePoint;
            }

            public String getUEmail() {
                return uEmail;
            }

            public void setUEmail(String uEmail) {
                this.uEmail = uEmail;
            }

            public String getUFindPwdCode() {
                return uFindPwdCode;
            }

            public void setUFindPwdCode(String uFindPwdCode) {
                this.uFindPwdCode = uFindPwdCode;
            }

            public int getUFindPwdCount() {
                return uFindPwdCount;
            }

            public void setUFindPwdCount(int uFindPwdCount) {
                this.uFindPwdCount = uFindPwdCount;
            }

            public String getUFindPwdTime() {
                return uFindPwdTime;
            }

            public void setUFindPwdTime(String uFindPwdTime) {
                this.uFindPwdTime = uFindPwdTime;
            }

            public int getUGgender() {
                return uGgender;
            }

            public void setUGgender(int uGgender) {
                this.uGgender = uGgender;
            }

            public int getUId() {
                return uId;
            }

            public void setUId(int uId) {
                this.uId = uId;
            }

            public int getUIsActive() {
                return uIsActive;
            }

            public void setUIsActive(int uIsActive) {
                this.uIsActive = uIsActive;
            }

            public String getULanguageType() {
                return uLanguageType;
            }

            public void setULanguageType(String uLanguageType) {
                this.uLanguageType = uLanguageType;
            }

            public int getULoginCount() {
                return uLoginCount;
            }

            public void setULoginCount(int uLoginCount) {
                this.uLoginCount = uLoginCount;
            }

            public String getUMobile() {
                return uMobile;
            }

            public void setUMobile(String uMobile) {
                this.uMobile = uMobile;
            }

            public String getUNickName() {
                return uNickName;
            }

            public void setUNickName(String uNickName) {
                this.uNickName = uNickName;
            }

            public int getUOccupation() {
                return uOccupation;
            }

            public void setUOccupation(int uOccupation) {
                this.uOccupation = uOccupation;
            }

            public String getUOpAddr() {
                return uOpAddr;
            }

            public void setUOpAddr(String uOpAddr) {
                this.uOpAddr = uOpAddr;
            }

            public String getUOpAddrBelong() {
                return uOpAddrBelong;
            }

            public void setUOpAddrBelong(String uOpAddrBelong) {
                this.uOpAddrBelong = uOpAddrBelong;
            }

            public String getUPersontag() {
                return uPersontag;
            }

            public void setUPersontag(String uPersontag) {
                this.uPersontag = uPersontag;
            }

            public String getUPinyin() {
                return uPinyin;
            }

            public void setUPinyin(String uPinyin) {
                this.uPinyin = uPinyin;
            }

            public String getUPortrait() {
                return uPortrait;
            }

            public void setUPortrait(String uPortrait) {
                this.uPortrait = uPortrait;
            }

            public String getUPositions() {
                return uPositions;
            }

            public void setUPositions(String uPositions) {
                this.uPositions = uPositions;
            }

            public String getUPwd() {
                return uPwd;
            }

            public void setUPwd(String uPwd) {
                this.uPwd = uPwd;
            }

            public String getURegTime() {
                return uRegTime;
            }

            public void setURegTime(String uRegTime) {
                this.uRegTime = uRegTime;
            }

            public String getUSchedule() {
                return uSchedule;
            }

            public void setUSchedule(String uSchedule) {
                this.uSchedule = uSchedule;
            }

            public int getUSourceType() {
                return uSourceType;
            }

            public void setUSourceType(int uSourceType) {
                this.uSourceType = uSourceType;
            }

            public String getUSpecialAccount() {
                return uSpecialAccount;
            }

            public void setUSpecialAccount(String uSpecialAccount) {
                this.uSpecialAccount = uSpecialAccount;
            }

            public String getUToCode() {
                return uToCode;
            }

            public void setUToCode(String uToCode) {
                this.uToCode = uToCode;
            }

            public String getUUpdateTime() {
                return uUpdateTime;
            }

            public void setUUpdateTime(String uUpdateTime) {
                this.uUpdateTime = uUpdateTime;
            }

            public Object getUValiTime() {
                return uValiTime;
            }

            public void setUValiTime(Object uValiTime) {
                this.uValiTime = uValiTime;
            }

            public String getUWeixinOpenid() {
                return uWeixinOpenid;
            }

            public void setUWeixinOpenid(String uWeixinOpenid) {
                this.uWeixinOpenid = uWeixinOpenid;
            }

            public String getUWeixinSign() {
                return uWeixinSign;
            }

            public void setUWeixinSign(String uWeixinSign) {
                this.uWeixinSign = uWeixinSign;
            }

            public int getUserIdOne() {
                return userIdOne;
            }

            public void setUserIdOne(int userIdOne) {
                this.userIdOne = userIdOne;
            }

            public int getUserIdTwo() {
                return userIdTwo;
            }

            public void setUserIdTwo(int userIdTwo) {
                this.userIdTwo = userIdTwo;
            }
        }
    }
}
